<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Post extends AdminController {
    
    public function __construct(){
        parent::__construct();
        date_default_timezone_set("America/Sao_Paulo");
    }

    public function index($id_menu = null, $page = 0 ){
        if (empty($id_menu)){
            $id_menu = $this->getDefaultMenu();
        }
        
        $findBy = array('menu' => $id_menu);
        $offset = 0;
        if( $page != 0 ) {
            $offset = ( $page - 1 ) * LIMITE_PAGINACAO_ADMIN;
        }

        $lista = $this->doctrine->em->getRepository( 'Posts' )->findBy(
            $findBy, array('id_post' => 'ASC'), LIMITE_PAGINACAO_ADMIN, $offset
        );
        
        $menu = $this->doctrine->em->getRepository( 'Menus' )->find($id_menu);

        $total = $this->countWithWhere('posts', array('id_menu' => $id_menu));
        
        $this->assign( 'lista', $lista );
        $this->assign( 'id_menu', $id_menu );
        $this->assign( 'id_menu_principal', $menu->get_menu_principal()->get_id_menu_principal() );
        
        $this->assign( 'total', $total );
        $this->display( 'post/index' );
    }
    
    public function editar($id = null) {
        if (!empty($id) && $_SERVER['REQUEST_METHOD'] == 'GET') {
            $obj = $this->doctrine->em->getRepository('Posts')->find($id);
            $_POST = $obj->asArray();
            if (!empty($obj)){
                $_POST['id_menu_principal'] = $obj->get_menu()->get_menu_principal()->get_id_menu_principal();
            }
        } else {
            $id_menu = $this->session->flashdata('id_menu');
            if (!empty($id_menu)){
                $menu = $this->doctrine->em->getRepository('Menus')->find($id_menu);
                $_POST['id_menu_principal'] = $menu->get_menu_principal()->get_id_menu_principal();
                $_POST['menu'] = $id_menu;
            }
            $_POST['ativo'] = true;
        }
        $this->display('post/editar');
    }
    
    
    public function novo($id_menu = null){
        $this->session->set_flashdata('id_menu', $id_menu);
        redirect('admin/post/editar');
    }
    
    public function save( $id = null ){
        $em = $this->doctrine->em;
        $entity = new Posts();
        try {
            if( !empty( $id ) ) {
                $entity = $em->getRepository( 'Posts' )->find( $id );
            }
            populateForm( $entity, $this->input->post() );
            $menu = $em->getRepository('Menus')->find($this->input->post('menu'));
            $usuario = $em->getRepository('Usuarios')->find($this->getUsuarioSessao()->get_id_usuario());
            $entity->set_menu($menu);
            if ($this->validate()){
                $entity->set_data_alteracao(new \DateTime( "now" ));
                $entity->set_usuario($usuario);
                $em->persist( $entity );
                $em->flush();
                $this->showSuccess();
                redirect('admin/post/index/'. $menu->get_id_menu());
            } 
        } catch (Exception $ex) {
            $this->showErros($ex);
        }         
        $this->display('post/editar');

    }
    
    private function validate(){
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('menu', 'Menu', 'required');
        $this->form_validation->set_rules('titulo', 'Título', 'required');
        $this->form_validation->set_rules('conteudo', 'Conteúdo', 'required');
        return $this->form_validation->run();
    }
    
    public function ajax_getMenus($id_menu_principal = 0){
        $data = array();
        $data['id_menu_principal'] = $id_menu_principal;
        echo $this->load->view('admin/post/components/combobox_menu', $data);
    }
    
    public function getMenus($id_menu_principal = 0){
        $result = array();
        $menus = $this->doctrine->em->getRepository( 'Menus' )->findBy(array('menu_principal' => $id_menu_principal), array('id_menu' => 'ASC'));
        foreach ($menus as $menu){
            $result[$menu->get_id_menu()] = $menu->get_titulo();
        }
        return $result;
    }
    
    ////////////////////////////////
    
    private function getDefaultMenu(){
        $sql = "select id_menu from menus order by id_menu limit 0,1 ";
        $stmt = $this->doctrine->em->getConnection()->prepare($sql);
        $stmt->execute();
        return $stmt->fetchColumn(0);
    }
        
}
/* End of file post.php */
/* Location: ./application/controllers/admin/post.php */